/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package meteocal.entity;

import java.io.Serializable;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import meteocal.utility.PasswordEncrypter;

/**
 *
 * @author andrea
 */
@Entity
public class Users implements Serializable {
    private static final long serialVersionUID = 1L;
    
    @Id
    @NotNull(message="Email cannot be empty")
    @Pattern(regexp="[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?",
            message="Invalid email")
    private String email;
    @NotNull(message="Password cannot be empty")
    private String password;
    @NotNull(message="Required field")
    private String groupName;
    @NotNull(message="Required field")
    private String name;
    @NotNull(message="Required field")
    private String surname;
    //@NotNull(message="Required field")
    private String address;
    //@NotNull(message="Required field")
    private String city;
    //@NotNull(message="Required field")
    @Temporal(TemporalType.DATE)
    private Calendar birthday;
    //@NotNull(message="Required field")
    private boolean privacy;
    
    //Self relationship between the user and his favorites
    @JoinTable(name = "FAVORITES",
            joinColumns = {@JoinColumn(name = "users", referencedColumnName = "email", nullable = false)},
            inverseJoinColumns = {@JoinColumn(name = "contact", referencedColumnName = "email", nullable = false)})
    @ManyToMany
    private List<Users> favorites;
    
    //Getters and Setters
    public String getEmail() {
        return email;
    }
    
    public void setEmail(String email) {
        this.email = email;
    }
    
    public String getPassword() {
        return password;
    }
    
    public void setPassword(String password) {
        this.password = PasswordEncrypter.encryptPassword(password);
    }
    
    public String getName() {
        return name;
    }
    
    public void setName(String name) {
        this.name = name;
    }
    
    public String getSurname() {
        return surname;
    }
    
    public void setSurname(String surname) {
        this.surname = surname;
    }
    
    public String getAddress() {
        return address;
    }
    
    public void setAddress(String address) {
        this.address = address;
    }
    
    public String getCity() {
        return city;
    }
    
    public void setCity(String city) {
        this.city = city;
    }
    
    public Calendar getBirthday() {
        return birthday;
    }
    
    public void setBirthday(Calendar birthday) {
        this.birthday = birthday;
    }
    
    public boolean getPrivacy() {
        return privacy;
    }
    
    public void setPrivacy(boolean privacy) {
        this.privacy = privacy;
    }
    
    public List<Users> getFavorites() {
        return favorites;
    }
    
    public void setFavorites(List<Users> favorites) {
        this.favorites = favorites;
    }
    
    public String getGroupName() {
        return groupName;
    }
    
    public void setGroupName(String groupName) {
        this.groupName = groupName;
    }  

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Users other = (Users) obj;
        
        return this.email.equals(other.email);
    }
    
    @Override
    public int hashCode() {
        return super.hashCode();
    }
    
    @Override
    public String toString() {
        return String.format("%s %s\t(%s)",name,surname,email);
    }
    
    public String nameSurnameString() {
        return String.format("%s %s",name,surname);
    }
        
}
